import { defineStore } from 'pinia';
//import { mine } from '@/enums/constants';
//import playApi from "@/apis/play";

export const playStore = defineStore('play', {
  state: () => {
    return {

      qualityIndex:1,
      qualityList:[],
      tabList: [
        {name: '热度榜',val:'HOT'},
        {name: '排行榜',val:'TOP'},
      ],
      rankCurrent:0,
      rankIndex:1,
      rankList: [],
      specialTabIndex:1,
      specialTabList:[]
    };
  },
  //persist:true,
  //多端处理
  /*persist:{
    enabled:true,
    storage:{
      setItem(key,value){
        uni.setStorageSync(key,value);
      },
      getItem(key){
        return uni.getStorageSync(key);
      }
    },
  },*/
  actions: {
    //获取精品列表
    async handleGetQualityList(type){
      let para = {
        pageIndex:this.qualityIndex,
        pageSize:mine.pageSize
      }
      const res = await playApi.getQualityList(para)
      if(res.code == 'SUCCESS'){
        if(type == 'scroll' && res.data?.length<=0){
          uni.showToast({
            icon:'none',
            title:'暂无数据了!'
          })
        }
        if(type == 'reset' && res.data?.length>=1){
          this.qualityList = res.data;
          return;
        }
        if(res.data?.length>=1){
          this.qualityList = this.qualityList.concat(res.data);
          this.qualityIndex++;
        }
      }
    },
    //清除精品数据
    clearQualityList(isClear) {
      this.qualityIndex= 1;
      if(isClear){
        this.qualityList = [];
      }
    },
    //获取榜单tab数据
    async handleGetRankTabList() {
      let para = {}
      const res = await playApi.getRankTabList(para)
      if(res.code == "SUCCESS"){
        if(!res.data || res.data?.length<=0){
          uni.showToast({
            icon:'error',
            title:'暂无榜单选项!'
          })
          return;
        }
        this.tabList = res.data;
      }
    },
    //获取榜单列表
    async handleGetRankList(option){
      let para = {
        pageIndex:this.rankIndex,
        pageSize:mine.pageSize,
        type:this.tabList[option.index ?? 0].val
      }
      const res = await playApi.getRankList(para);
      if(res.code == 'SUCCESS'){
        if(option.type == 'scroll' && res.data?.length<=0){
          uni.showToast({
            icon:'none',
            title:'暂无数据了!'
          })
        }
        if(option.type == 'reset' && res.data?.length>=1){
          this.rankList = res.data;
          return;
        }
        if(res.data?.length>=1){
          this.rankList = this.rankList.concat(res.data);
          this.rankIndex++;
        }
      }
    },
    //清除榜单数据
    clearRankList(isClear) {
      this.rankCurrent = 0;
      this.rankIndex= 1;
      if(isClear){
        this.rankList = [];
      }
    },
    //修改榜单列表的收藏数值
    setRankListCollect(curIndex,val){
      this.rankList[curIndex].BookMarked = val;
    },

    //获取专题Tab列表
    async handleGetSpecialTabList(type){
      let para = {
        //pageIndex:this.specialTabIndex,
        //pageSize:mine.pageSize,
      }
      const res = await playApi.getSpecialTabList(para);
      if(res.code == 'SUCCESS'){
        if(type == 'scroll' && res.data?.length<=0){
          uni.showToast({
            icon:'none',
            title:'暂无数据了!'
          })
        }
        if(type == 'reset' && res.data?.length>=1){
          this.specialTabList = res.data;
          return;
        }
        if(res.data?.length>=1){
          this.specialTabList = this.specialTabList.concat(res.data);
          this.specialTabIndex++;
        }
      }
    },
    //清除专题数据
    clearSpecialTabList(isClear) {
      this.specialTabIndex= 1;
      if(isClear){
        this.specialTabList = [];
      }
    },
  }
});
